﻿//-----------------------------------------------------------------------
// <copyright file="OrderStatus.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Delta.Mercury.Business.Commerce
{
    using System;
    using Delta.Data.DataExpress;
    using Delta.Mercury.Business.Exceptions;

    /// <summary>
    /// Order status type
    /// </summary>
    [Serializable]
    public class OrderStatus : IdentifierBase, IDataServicable
    {
        #region Members
        /// <summary>
        /// Order status name
        /// </summary>
        private string name;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the OrderStatus class.
        /// </summary>
        public OrderStatus()
            : this(string.Empty)
        {         
        }

        /// <summary>
        /// Initializes a new instance of the OrderStatus class.
        /// </summary>
        /// <param name="statusName">Status name</param>
        public OrderStatus(string statusName)
            : base()
        {
            this.name = statusName;
            this.SortOrder = 0;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the order status name.
        /// </summary>
        public virtual string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                if (value.Length < 1)
                {
                    throw new InvalidLengthException("Order status name must be a minimum of 1 characters.");
                }

                if (value.Length > 20)
                {
                    throw new InvalidLengthException("Order status name must be a maximum of 20 characters.");
                }

                this.name = value;
            }
        }

        /// <summary>
        /// Gets or sets the order the item shows
        /// </summary>
        public virtual int SortOrder { get; set; }
        #endregion

        #region Load Methods
        /// <summary>
        /// Load order status by unique identifier
        /// </summary>
        /// <param name="guid">Unique identifier</param>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Order status object</returns>
        public static OrderStatus Load(Guid guid, bool showDeleted)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", guid));
            parameters.Add(new Parameter("Deleted", showDeleted));
            return DataAccess.GetSingle<OrderStatus>("LoadOrderStatus", System.Data.CommandType.StoredProcedure, parameters);
        }

        /// <summary>
        /// Load order status by name
        /// </summary>
        /// <param name="name">Status name</param>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Order status object</returns>
        public static OrderStatus Load(string name, bool showDeleted)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Name", name));
            parameters.Add(new Parameter("Deleted", showDeleted));
            return DataAccess.GetSingle<OrderStatus>("LoadOrderStatus", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Delete Methods
        /// <summary>
        /// Delete order status
        /// </summary>
        public virtual void Delete()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            DataAccess.NonQuery("DeleteOrderStatus", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Save Methods
        /// <summary>
        /// Save the order status
        /// </summary>
        public virtual void Save()
        {
            DataAccess.NonQuery("SaveOrderStatus", System.Data.CommandType.StoredProcedure, GetSaveParameters());
        }
        #endregion

        #region Fill Methods
        /// <summary>
        /// Fill order status from database
        /// </summary>
        /// <param name="reader">Data from database</param>
        public virtual void Fill(System.Data.IDataReader reader)
        {
            this.Guid = Helper.ToGuid(reader["Guid"]);
            this.name = Helper.ToString(reader["Name"]);
            this.SortOrder = Helper.ToInt(reader["SortOrder"]);
        }
        #endregion

        #region Get Save Parameters
        /// <summary>
        /// Get save parameters
        /// </summary>
        /// <returns>Parameter collection</returns>
        public Parameters GetSaveParameters()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            parameters.Add(new Parameter("Name", this.Name));
            parameters.Add(new Parameter("SortOrder", this.SortOrder));
            return parameters;
        }
        #endregion
    }
}
